﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using ExtendModule;

namespace LabMS.Web.Equipment.Consum
{

    public partial class consumlist : Base.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            strPagePrivilege.Add("HCGL");//必须写在ispostback外
            strPageUserType.Add("mana");
            strPageUserType.Add("teacher");
            strPageUserType.Add("student");

            tbCreateDateMin.Attributes.Add("onclick", "c.showMoreDay = false;c.show(this)");
            tbCreateDateMax.Attributes.Add("onclick", "c.showMoreDay = false;c.show(this)");
            if (!Page.IsPostBack)
            {
                bind("");
                btDel.Attributes.Add("onclick", "javascript:return confirm('你确认要删除这些易耗品吗?')");
                if (UserType != "mana")
                {
                    btDel.Visible = false;
                }
            }
            tbCreateDateMin.Attributes.Add("readOnly", "-1");
            tbCreateDateMax.Attributes.Add("readOnly", "-1");
        }

        public void bind(string strWhere)
        {
            List<ExtendModule.InstrumentAndConsumables> listeconsummodule = new
                List<InstrumentAndConsumables>();
            ExtendBLL.InstrumentAndConsumables beconsum = new ExtendBLL.InstrumentAndConsumables();
            listeconsummodule = beconsum.GetExtendModelList(" Type = 'xiaohaopin' " + strWhere + " order by ID desc ");
            GridViewConsum.DataSource = listeconsummodule;
            GridViewConsum.DataBind();
        }

        protected void GridViewConsum_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridViewConsum.PageIndex = e.NewPageIndex;
            if (tbKeyWords.Text.Trim().Equals("") && tbCreateDateMin.Text.Trim().Equals("") &&
                     tbCreateDateMax.Text.Trim().Equals("") && tbPurpose.Text.Trim().Equals(""))
            {
                bind("");
            }
            else
            {
                bind(SearchString());
            }
        }

        protected void GridViewConsum_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes["style"] = "cursor:pointer";
                int itemp = e.Row.Cells.Count;
                for (int i = 1; i < itemp; i++)
                {
                    e.Row.Cells[i].Attributes.Add("OnDblClick", "window.location.href='consumpreview.aspx?ID="
                                        + GridViewConsum.DataKeys[GridViewConsum.DataKeys.Count - 1].Value.ToString() + "'");
                }
            }
        }

        //搜索查询语句
        protected string SearchString()
        {
            string strWhere = " and " + ddlCClass.SelectedValue + " like '%" + tbKeyWords.Text.Trim() + "%' ";
            if (!string.IsNullOrEmpty(tbCreateDateMin.Text.Trim()))
            {
                strWhere += " and CreatedDate >= '" + tbCreateDateMin.Text.Trim() + "'";
            }
            if (!string.IsNullOrEmpty(tbCreateDateMax.Text.Trim()))
            {
                strWhere += " and CreatedDate <= '" + tbCreateDateMax.Text.Trim() + "'";
            }
            strWhere += " and Purpose like '%" + tbPurpose.Text.Trim() + "%'";

            return strWhere;
        }

        protected void btSearch_Click(object sender, EventArgs e)
        {
            bind(SearchString());
        }

        protected void btUpdate_Click(object sender, EventArgs e)
        {
            tbKeyWords.Text = "";
            tbPurpose.Text = "";
            tbCreateDateMin.Text = "";
            tbCreateDateMax.Text = "";
            bind("");
        }

        protected void btDel_Click(object sender, EventArgs e)
        {
            lbErr.Visible = false;
            CheckBox ch;
            LabMS.BLL.InstrumentAndConsumables bconsum = new LabMS.BLL.InstrumentAndConsumables();
            try
            {
                for (int i = 0; i < GridViewConsum.Rows.Count; i++)
                {
                    ch = (CheckBox)GridViewConsum.Rows[i].FindControl("checkdel");
                    if (ch.Checked)
                    {
                        string CID = GridViewConsum.DataKeys[i].Value.ToString();
                        bconsum.Delete(long.Parse(CID));
                    }
                }
                //checkall.Checked = false;
                if (tbKeyWords.Text.Trim().Equals("") && tbCreateDateMin.Text.Trim().Equals("") &&
                     tbCreateDateMax.Text.Trim().Equals("") && tbPurpose.Text.Trim().Equals(""))
                {
                    bind("");
                }
                else
                {
                    bind(SearchString());
                }

                Common.JShelper.JSAlert(Page, "success", "删除成功!");
            }
            catch (Exception ex)
            {
                Common.JShelper.JSAlert(Page, "err", "删除失败!");
                lbErr.Visible = true;
                lbErr.Text = ex.Message;
            }
        }

        protected void GridViewConsum_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (UserType != "mana")
            {
                if (e.Row.RowType != DataControlRowType.EmptyDataRow && e.Row.RowType != DataControlRowType.Pager)
                {
                    e.Row.Cells[0].Visible = false;
                }
            }
        }
    }
}
